{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.0500</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>95.7900</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>126000</td>\n",
       "      <td>4.940000e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.3000</td>\n",
       "      <td>104.3900</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>104.3900</td>\n",
       "      <td>19700</td>\n",
       "      <td>8.400000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.0700</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>103.7300</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>2800</td>\n",
       "      <td>1.600000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.5700</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>3200</td>\n",
       "      <td>3.100000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.0900</td>\n",
       "      <td>120.2500</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>120.2500</td>\n",
       "      <td>1500</td>\n",
       "      <td>6.000000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8210 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1990-12-19               96.0500    99.9800    95.7900    99.9800   \n",
       "1990-12-20      99.98   104.3000   104.3900    99.9800   104.3900   \n",
       "1990-12-21     104.39   109.0700   109.1300   103.7300   109.1300   \n",
       "1990-12-24     109.13   113.5700   114.5500   109.1300   114.5500   \n",
       "1990-12-25     114.55   120.0900   120.2500   114.5500   120.2500   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money  \n",
       "Day                                    \n",
       "1990-12-19       126000  4.940000e+05  \n",
       "1990-12-20        19700  8.400000e+04  \n",
       "1990-12-21         2800  1.600000e+04  \n",
       "1990-12-24         3200  3.100000e+04  \n",
       "1990-12-25         1500  6.000000e+03  \n",
       "...                 ...           ...  \n",
       "2024-07-25  27463950000  2.732820e+11  \n",
       "2024-07-26  27838753600  2.754430e+11  \n",
       "2024-07-29  25689972700  2.600950e+11  \n",
       "2024-07-30  26247883700  2.694770e+11  \n",
       "2024-07-31  41272341700  4.188720e+11  \n",
       "\n",
       "[8210 rows x 7 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('000001.csv')\n",
    "data['Day'] = pd.to_datetime(data['Day'],format='%Y/%m/%d')\n",
    "#把日变成日期格式，选择变动的那一列，按照数据原来的格式设置相应格式\n",
    "data.set_index('Day',inplace=True)\n",
    "#把日期设置成索引\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意：由于已默认，以上数据处理省去排序这一步骤"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算日收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1996-01-02</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>550.2600</td>\n",
       "      <td>550.2600</td>\n",
       "      <td>537.3800</td>\n",
       "      <td>537.8700</td>\n",
       "      <td>48247800</td>\n",
       "      <td>2.231170e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-03</th>\n",
       "      <td>537.8700</td>\n",
       "      <td>535.2300</td>\n",
       "      <td>542.7400</td>\n",
       "      <td>530.7900</td>\n",
       "      <td>542.4200</td>\n",
       "      <td>55619200</td>\n",
       "      <td>2.525740e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-04</th>\n",
       "      <td>542.4200</td>\n",
       "      <td>541.9400</td>\n",
       "      <td>558.9400</td>\n",
       "      <td>539.7600</td>\n",
       "      <td>558.7600</td>\n",
       "      <td>110591300</td>\n",
       "      <td>4.514880e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-05</th>\n",
       "      <td>558.7600</td>\n",
       "      <td>559.0300</td>\n",
       "      <td>561.3600</td>\n",
       "      <td>536.1200</td>\n",
       "      <td>536.3700</td>\n",
       "      <td>138637100</td>\n",
       "      <td>5.971850e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-08</th>\n",
       "      <td>536.3700</td>\n",
       "      <td>533.3300</td>\n",
       "      <td>540.0400</td>\n",
       "      <td>527.9100</td>\n",
       "      <td>539.1700</td>\n",
       "      <td>69620700</td>\n",
       "      <td>2.871500e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-26</th>\n",
       "      <td>3197.9011</td>\n",
       "      <td>3177.2293</td>\n",
       "      <td>3181.0758</td>\n",
       "      <td>3144.2484</td>\n",
       "      <td>3150.6189</td>\n",
       "      <td>30812981100</td>\n",
       "      <td>3.990000e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-27</th>\n",
       "      <td>3150.6189</td>\n",
       "      <td>3153.3132</td>\n",
       "      <td>3194.4086</td>\n",
       "      <td>3148.2657</td>\n",
       "      <td>3189.4427</td>\n",
       "      <td>28760432700</td>\n",
       "      <td>3.540000e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-28</th>\n",
       "      <td>3189.4427</td>\n",
       "      <td>3183.4865</td>\n",
       "      <td>3192.6589</td>\n",
       "      <td>3157.1229</td>\n",
       "      <td>3189.3758</td>\n",
       "      <td>27623194800</td>\n",
       "      <td>3.600000e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-29</th>\n",
       "      <td>3189.3758</td>\n",
       "      <td>3185.4242</td>\n",
       "      <td>3196.5025</td>\n",
       "      <td>3179.5251</td>\n",
       "      <td>3182.3812</td>\n",
       "      <td>25034007400</td>\n",
       "      <td>3.410000e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-30</th>\n",
       "      <td>3182.3812</td>\n",
       "      <td>3178.9242</td>\n",
       "      <td>3212.9928</td>\n",
       "      <td>3177.9913</td>\n",
       "      <td>3202.0623</td>\n",
       "      <td>26537987300</td>\n",
       "      <td>3.600000e+11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6667 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1996-01-02   555.2900   550.2600   550.2600   537.3800   537.8700   \n",
       "1996-01-03   537.8700   535.2300   542.7400   530.7900   542.4200   \n",
       "1996-01-04   542.4200   541.9400   558.9400   539.7600   558.7600   \n",
       "1996-01-05   558.7600   559.0300   561.3600   536.1200   536.3700   \n",
       "1996-01-08   536.3700   533.3300   540.0400   527.9100   539.1700   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2023-06-26  3197.9011  3177.2293  3181.0758  3144.2484  3150.6189   \n",
       "2023-06-27  3150.6189  3153.3132  3194.4086  3148.2657  3189.4427   \n",
       "2023-06-28  3189.4427  3183.4865  3192.6589  3157.1229  3189.3758   \n",
       "2023-06-29  3189.3758  3185.4242  3196.5025  3179.5251  3182.3812   \n",
       "2023-06-30  3182.3812  3178.9242  3212.9928  3177.9913  3202.0623   \n",
       "\n",
       "                 Volume         Money  \n",
       "Day                                    \n",
       "1996-01-02     48247800  2.231170e+08  \n",
       "1996-01-03     55619200  2.525740e+08  \n",
       "1996-01-04    110591300  4.514880e+08  \n",
       "1996-01-05    138637100  5.971850e+08  \n",
       "1996-01-08     69620700  2.871500e+08  \n",
       "...                 ...           ...  \n",
       "2023-06-26  30812981100  3.990000e+11  \n",
       "2023-06-27  28760432700  3.540000e+11  \n",
       "2023-06-28  27623194800  3.600000e+11  \n",
       "2023-06-29  25034007400  3.410000e+11  \n",
       "2023-06-30  26537987300  3.600000e+11  \n",
       "\n",
       "[6667 rows x 7 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new = data['1996':'2023-06'].copy()\n",
    "data_new['Close'] = pd.to_numeric(data_new['Close'])\n",
    "data_new['Preclose']=pd.to_numeric(data_new['Preclose'])\n",
    "#使用pd.to_numeric将字符串变成数字\n",
    "data_new"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.向量化的数据处理方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return _2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1996-01-02</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>550.2600</td>\n",
       "      <td>550.2600</td>\n",
       "      <td>537.3800</td>\n",
       "      <td>537.8700</td>\n",
       "      <td>48247800</td>\n",
       "      <td>2.231170e+08</td>\n",
       "      <td>-0.031371</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-03</th>\n",
       "      <td>537.8700</td>\n",
       "      <td>535.2300</td>\n",
       "      <td>542.7400</td>\n",
       "      <td>530.7900</td>\n",
       "      <td>542.4200</td>\n",
       "      <td>55619200</td>\n",
       "      <td>2.525740e+08</td>\n",
       "      <td>0.008459</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-04</th>\n",
       "      <td>542.4200</td>\n",
       "      <td>541.9400</td>\n",
       "      <td>558.9400</td>\n",
       "      <td>539.7600</td>\n",
       "      <td>558.7600</td>\n",
       "      <td>110591300</td>\n",
       "      <td>4.514880e+08</td>\n",
       "      <td>0.030124</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-05</th>\n",
       "      <td>558.7600</td>\n",
       "      <td>559.0300</td>\n",
       "      <td>561.3600</td>\n",
       "      <td>536.1200</td>\n",
       "      <td>536.3700</td>\n",
       "      <td>138637100</td>\n",
       "      <td>5.971850e+08</td>\n",
       "      <td>-0.040071</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-08</th>\n",
       "      <td>536.3700</td>\n",
       "      <td>533.3300</td>\n",
       "      <td>540.0400</td>\n",
       "      <td>527.9100</td>\n",
       "      <td>539.1700</td>\n",
       "      <td>69620700</td>\n",
       "      <td>2.871500e+08</td>\n",
       "      <td>0.005220</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-26</th>\n",
       "      <td>3197.9011</td>\n",
       "      <td>3177.2293</td>\n",
       "      <td>3181.0758</td>\n",
       "      <td>3144.2484</td>\n",
       "      <td>3150.6189</td>\n",
       "      <td>30812981100</td>\n",
       "      <td>3.990000e+11</td>\n",
       "      <td>-0.014785</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-27</th>\n",
       "      <td>3150.6189</td>\n",
       "      <td>3153.3132</td>\n",
       "      <td>3194.4086</td>\n",
       "      <td>3148.2657</td>\n",
       "      <td>3189.4427</td>\n",
       "      <td>28760432700</td>\n",
       "      <td>3.540000e+11</td>\n",
       "      <td>0.012323</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-28</th>\n",
       "      <td>3189.4427</td>\n",
       "      <td>3183.4865</td>\n",
       "      <td>3192.6589</td>\n",
       "      <td>3157.1229</td>\n",
       "      <td>3189.3758</td>\n",
       "      <td>27623194800</td>\n",
       "      <td>3.600000e+11</td>\n",
       "      <td>-0.000021</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-29</th>\n",
       "      <td>3189.3758</td>\n",
       "      <td>3185.4242</td>\n",
       "      <td>3196.5025</td>\n",
       "      <td>3179.5251</td>\n",
       "      <td>3182.3812</td>\n",
       "      <td>25034007400</td>\n",
       "      <td>3.410000e+11</td>\n",
       "      <td>-0.002193</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-30</th>\n",
       "      <td>3182.3812</td>\n",
       "      <td>3178.9242</td>\n",
       "      <td>3212.9928</td>\n",
       "      <td>3177.9913</td>\n",
       "      <td>3202.0623</td>\n",
       "      <td>26537987300</td>\n",
       "      <td>3.600000e+11</td>\n",
       "      <td>0.006184</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6667 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1996-01-02   555.2900   550.2600   550.2600   537.3800   537.8700   \n",
       "1996-01-03   537.8700   535.2300   542.7400   530.7900   542.4200   \n",
       "1996-01-04   542.4200   541.9400   558.9400   539.7600   558.7600   \n",
       "1996-01-05   558.7600   559.0300   561.3600   536.1200   536.3700   \n",
       "1996-01-08   536.3700   533.3300   540.0400   527.9100   539.1700   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2023-06-26  3197.9011  3177.2293  3181.0758  3144.2484  3150.6189   \n",
       "2023-06-27  3150.6189  3153.3132  3194.4086  3148.2657  3189.4427   \n",
       "2023-06-28  3189.4427  3183.4865  3192.6589  3157.1229  3189.3758   \n",
       "2023-06-29  3189.3758  3185.4242  3196.5025  3179.5251  3182.3812   \n",
       "2023-06-30  3182.3812  3178.9242  3212.9928  3177.9913  3202.0623   \n",
       "\n",
       "                 Volume         Money    Return  Return _2  \n",
       "Day                                                         \n",
       "1996-01-02     48247800  2.231170e+08 -0.031371          0  \n",
       "1996-01-03     55619200  2.525740e+08  0.008459          0  \n",
       "1996-01-04    110591300  4.514880e+08  0.030124          0  \n",
       "1996-01-05    138637100  5.971850e+08 -0.040071          0  \n",
       "1996-01-08     69620700  2.871500e+08  0.005220          0  \n",
       "...                 ...           ...       ...        ...  \n",
       "2023-06-26  30812981100  3.990000e+11 -0.014785          0  \n",
       "2023-06-27  28760432700  3.540000e+11  0.012323          0  \n",
       "2023-06-28  27623194800  3.600000e+11 -0.000021          0  \n",
       "2023-06-29  25034007400  3.410000e+11 -0.002193          0  \n",
       "2023-06-30  26537987300  3.600000e+11  0.006184          0  \n",
       "\n",
       "[6667 rows x 9 columns]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Return'] =( data_new['Close'] / data_new['Preclose'])-1\n",
    "#重新添加一列命名为Return，表示日回报率\n",
    "data_new['Return _2'] = 0\n",
    "data_new"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.错误示范：使用for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21230\\AppData\\Local\\Temp\\ipykernel_7780\\4011317476.py:2: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  data_new['Return _2'][i] =(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
      "C:\\Users\\21230\\AppData\\Local\\Temp\\ipykernel_7780\\4011317476.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  data_new['Return _2'][i] =(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
      "C:\\Users\\21230\\AppData\\Local\\Temp\\ipykernel_7780\\4011317476.py:2: FutureWarning: Series.__setitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To set a value by position, use `ser.iloc[pos] = value`\n",
      "  data_new['Return _2'][i] =(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
      "C:\\Users\\21230\\AppData\\Local\\Temp\\ipykernel_7780\\4011317476.py:2: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '-0.03137099533577048' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.\n",
      "  data_new['Return _2'][i] =(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
      "C:\\Users\\21230\\AppData\\Local\\Temp\\ipykernel_7780\\4011317476.py:2: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  data_new['Return _2'][i] =(data_new['Close'][i]/data_new['Preclose'][i])-1\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return _2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1996-01-02</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>550.2600</td>\n",
       "      <td>550.2600</td>\n",
       "      <td>537.3800</td>\n",
       "      <td>537.8700</td>\n",
       "      <td>48247800</td>\n",
       "      <td>2.231170e+08</td>\n",
       "      <td>-0.031371</td>\n",
       "      <td>-0.031371</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-03</th>\n",
       "      <td>537.8700</td>\n",
       "      <td>535.2300</td>\n",
       "      <td>542.7400</td>\n",
       "      <td>530.7900</td>\n",
       "      <td>542.4200</td>\n",
       "      <td>55619200</td>\n",
       "      <td>2.525740e+08</td>\n",
       "      <td>0.008459</td>\n",
       "      <td>0.008459</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-04</th>\n",
       "      <td>542.4200</td>\n",
       "      <td>541.9400</td>\n",
       "      <td>558.9400</td>\n",
       "      <td>539.7600</td>\n",
       "      <td>558.7600</td>\n",
       "      <td>110591300</td>\n",
       "      <td>4.514880e+08</td>\n",
       "      <td>0.030124</td>\n",
       "      <td>0.030124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-05</th>\n",
       "      <td>558.7600</td>\n",
       "      <td>559.0300</td>\n",
       "      <td>561.3600</td>\n",
       "      <td>536.1200</td>\n",
       "      <td>536.3700</td>\n",
       "      <td>138637100</td>\n",
       "      <td>5.971850e+08</td>\n",
       "      <td>-0.040071</td>\n",
       "      <td>-0.040071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-01-08</th>\n",
       "      <td>536.3700</td>\n",
       "      <td>533.3300</td>\n",
       "      <td>540.0400</td>\n",
       "      <td>527.9100</td>\n",
       "      <td>539.1700</td>\n",
       "      <td>69620700</td>\n",
       "      <td>2.871500e+08</td>\n",
       "      <td>0.005220</td>\n",
       "      <td>0.005220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-26</th>\n",
       "      <td>3197.9011</td>\n",
       "      <td>3177.2293</td>\n",
       "      <td>3181.0758</td>\n",
       "      <td>3144.2484</td>\n",
       "      <td>3150.6189</td>\n",
       "      <td>30812981100</td>\n",
       "      <td>3.990000e+11</td>\n",
       "      <td>-0.014785</td>\n",
       "      <td>-0.014785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-27</th>\n",
       "      <td>3150.6189</td>\n",
       "      <td>3153.3132</td>\n",
       "      <td>3194.4086</td>\n",
       "      <td>3148.2657</td>\n",
       "      <td>3189.4427</td>\n",
       "      <td>28760432700</td>\n",
       "      <td>3.540000e+11</td>\n",
       "      <td>0.012323</td>\n",
       "      <td>0.012323</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-28</th>\n",
       "      <td>3189.4427</td>\n",
       "      <td>3183.4865</td>\n",
       "      <td>3192.6589</td>\n",
       "      <td>3157.1229</td>\n",
       "      <td>3189.3758</td>\n",
       "      <td>27623194800</td>\n",
       "      <td>3.600000e+11</td>\n",
       "      <td>-0.000021</td>\n",
       "      <td>-0.000021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-29</th>\n",
       "      <td>3189.3758</td>\n",
       "      <td>3185.4242</td>\n",
       "      <td>3196.5025</td>\n",
       "      <td>3179.5251</td>\n",
       "      <td>3182.3812</td>\n",
       "      <td>25034007400</td>\n",
       "      <td>3.410000e+11</td>\n",
       "      <td>-0.002193</td>\n",
       "      <td>-0.002193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-30</th>\n",
       "      <td>3182.3812</td>\n",
       "      <td>3178.9242</td>\n",
       "      <td>3212.9928</td>\n",
       "      <td>3177.9913</td>\n",
       "      <td>3202.0623</td>\n",
       "      <td>26537987300</td>\n",
       "      <td>3.600000e+11</td>\n",
       "      <td>0.006184</td>\n",
       "      <td>0.006184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6667 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1996-01-02   555.2900   550.2600   550.2600   537.3800   537.8700   \n",
       "1996-01-03   537.8700   535.2300   542.7400   530.7900   542.4200   \n",
       "1996-01-04   542.4200   541.9400   558.9400   539.7600   558.7600   \n",
       "1996-01-05   558.7600   559.0300   561.3600   536.1200   536.3700   \n",
       "1996-01-08   536.3700   533.3300   540.0400   527.9100   539.1700   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2023-06-26  3197.9011  3177.2293  3181.0758  3144.2484  3150.6189   \n",
       "2023-06-27  3150.6189  3153.3132  3194.4086  3148.2657  3189.4427   \n",
       "2023-06-28  3189.4427  3183.4865  3192.6589  3157.1229  3189.3758   \n",
       "2023-06-29  3189.3758  3185.4242  3196.5025  3179.5251  3182.3812   \n",
       "2023-06-30  3182.3812  3178.9242  3212.9928  3177.9913  3202.0623   \n",
       "\n",
       "                 Volume         Money    Return  Return _2  \n",
       "Day                                                         \n",
       "1996-01-02     48247800  2.231170e+08 -0.031371  -0.031371  \n",
       "1996-01-03     55619200  2.525740e+08  0.008459   0.008459  \n",
       "1996-01-04    110591300  4.514880e+08  0.030124   0.030124  \n",
       "1996-01-05    138637100  5.971850e+08 -0.040071  -0.040071  \n",
       "1996-01-08     69620700  2.871500e+08  0.005220   0.005220  \n",
       "...                 ...           ...       ...        ...  \n",
       "2023-06-26  30812981100  3.990000e+11 -0.014785  -0.014785  \n",
       "2023-06-27  28760432700  3.540000e+11  0.012323   0.012323  \n",
       "2023-06-28  27623194800  3.600000e+11 -0.000021  -0.000021  \n",
       "2023-06-29  25034007400  3.410000e+11 -0.002193  -0.002193  \n",
       "2023-06-30  26537987300  3.600000e+11  0.006184   0.006184  \n",
       "\n",
       "[6667 rows x 9 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range (0,len(data_new)):\n",
    "    data_new['Return _2'][i] =(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
    "\n",
    "data_new\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 两种计算方式的差别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "两种计算方式的差别： 0.0\n"
     ]
    }
   ],
   "source": [
    "data_new['diff'] = data_new['Return']-data_new['Return _2']\n",
    "print('两种计算方式的差别：',data_new['diff'].sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return _2</th>\n",
       "      <th>diff</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [Preclose, Open, Highest, Lowest, Close, Volume, Money, Return, Return _2, diff]\n",
       "Index: []"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new[data_new['Return']>0.11]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return _2</th>\n",
       "      <th>diff</th>\n",
       "      <th>Return_plus1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [Preclose, Open, Highest, Lowest, Close, Volume, Money, Return, Return _2, diff, Return_plus1]\n",
       "Index: []"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new[data_new['Return']<-0.13]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算上证综指月度、季度、年度的收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 月度收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$t$表示月份\n",
    "$$\n",
    "R_{t}=\\frac{P_{close,t}-P_{close,t-1}}{P_{close,t-1}}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "逻辑：下脚标用下划线加{}表示，frac表示上下分离"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "/frac{分子}{分母}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 算法一\n",
    "\n",
    "1.data_new日度数据里面选择月的最后一天close"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.把这个月close变成上个月的最后一天"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.计算月度收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1996-01-31</th>\n",
       "      <td>537.3500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-02-29</th>\n",
       "      <td>552.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>556.3900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-04-30</th>\n",
       "      <td>681.1600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-05-31</th>\n",
       "      <td>643.6500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-02-28</th>\n",
       "      <td>3279.6053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-03-31</th>\n",
       "      <td>3272.8602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04-30</th>\n",
       "      <td>3323.2746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-05-31</th>\n",
       "      <td>3204.5644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-30</th>\n",
       "      <td>3202.0623</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>330 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close\n",
       "Day                  \n",
       "1996-01-31   537.3500\n",
       "1996-02-29   552.9400\n",
       "1996-03-31   556.3900\n",
       "1996-04-30   681.1600\n",
       "1996-05-31   643.6500\n",
       "...               ...\n",
       "2023-02-28  3279.6053\n",
       "2023-03-31  3272.8602\n",
       "2023-04-30  3323.2746\n",
       "2023-05-31  3204.5644\n",
       "2023-06-30  3202.0623\n",
       "\n",
       "[330 rows x 1 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data=data_new.resample('M')['Close'].last().to_frame()\n",
    "#resample重新取样，M按月\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1996-01-31</th>\n",
       "      <td>537.3500</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-02-29</th>\n",
       "      <td>552.9400</td>\n",
       "      <td>537.3500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>556.3900</td>\n",
       "      <td>552.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-04-30</th>\n",
       "      <td>681.1600</td>\n",
       "      <td>556.3900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-05-31</th>\n",
       "      <td>643.6500</td>\n",
       "      <td>681.1600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-02-28</th>\n",
       "      <td>3279.6053</td>\n",
       "      <td>3255.6692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-03-31</th>\n",
       "      <td>3272.8602</td>\n",
       "      <td>3279.6053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04-30</th>\n",
       "      <td>3323.2746</td>\n",
       "      <td>3272.8602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-05-31</th>\n",
       "      <td>3204.5644</td>\n",
       "      <td>3323.2746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-30</th>\n",
       "      <td>3202.0623</td>\n",
       "      <td>3204.5644</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>330 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose\n",
       "Day                             \n",
       "1996-01-31   537.3500        NaN\n",
       "1996-02-29   552.9400   537.3500\n",
       "1996-03-31   556.3900   552.9400\n",
       "1996-04-30   681.1600   556.3900\n",
       "1996-05-31   643.6500   681.1600\n",
       "...               ...        ...\n",
       "2023-02-28  3279.6053  3255.6692\n",
       "2023-03-31  3272.8602  3279.6053\n",
       "2023-04-30  3323.2746  3272.8602\n",
       "2023-05-31  3204.5644  3323.2746\n",
       "2023-06-30  3202.0623  3204.5644\n",
       "\n",
       "[330 rows x 2 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data['Preclose']=Month_data['Close'].shift(1)\n",
    "#这里Preclose从shif(1)开始取值对应着从Day那一行开始，所以往下每一行都是错位的！！！\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1996-01-31</th>\n",
       "      <td>537.3500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-02-29</th>\n",
       "      <td>552.9400</td>\n",
       "      <td>537.3500</td>\n",
       "      <td>0.029013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>556.3900</td>\n",
       "      <td>552.9400</td>\n",
       "      <td>0.006239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-04-30</th>\n",
       "      <td>681.1600</td>\n",
       "      <td>556.3900</td>\n",
       "      <td>0.224249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-05-31</th>\n",
       "      <td>643.6500</td>\n",
       "      <td>681.1600</td>\n",
       "      <td>-0.055068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-02-28</th>\n",
       "      <td>3279.6053</td>\n",
       "      <td>3255.6692</td>\n",
       "      <td>0.007352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-03-31</th>\n",
       "      <td>3272.8602</td>\n",
       "      <td>3279.6053</td>\n",
       "      <td>-0.002057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04-30</th>\n",
       "      <td>3323.2746</td>\n",
       "      <td>3272.8602</td>\n",
       "      <td>0.015404</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-05-31</th>\n",
       "      <td>3204.5644</td>\n",
       "      <td>3323.2746</td>\n",
       "      <td>-0.035721</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-30</th>\n",
       "      <td>3202.0623</td>\n",
       "      <td>3204.5644</td>\n",
       "      <td>-0.000781</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>330 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose    Return\n",
       "Day                                       \n",
       "1996-01-31   537.3500        NaN       NaN\n",
       "1996-02-29   552.9400   537.3500  0.029013\n",
       "1996-03-31   556.3900   552.9400  0.006239\n",
       "1996-04-30   681.1600   556.3900  0.224249\n",
       "1996-05-31   643.6500   681.1600 -0.055068\n",
       "...               ...        ...       ...\n",
       "2023-02-28  3279.6053  3255.6692  0.007352\n",
       "2023-03-31  3272.8602  3279.6053 -0.002057\n",
       "2023-04-30  3323.2746  3272.8602  0.015404\n",
       "2023-05-31  3204.5644  3323.2746 -0.035721\n",
       "2023-06-30  3202.0623  3204.5644 -0.000781\n",
       "\n",
       "[330 rows x 3 columns]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data['Return']=(Month_data['Close']/Month_data['Preclose'])-1\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 算法二\n",
    "按天进行累计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Return_plus1</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1996-01-31</th>\n",
       "      <td>0.967693</td>\n",
       "      <td>-0.032307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-02-29</th>\n",
       "      <td>1.029013</td>\n",
       "      <td>0.029013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>1.006239</td>\n",
       "      <td>0.006239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-04-30</th>\n",
       "      <td>1.224249</td>\n",
       "      <td>0.224249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-05-31</th>\n",
       "      <td>0.944932</td>\n",
       "      <td>-0.055068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-02-28</th>\n",
       "      <td>1.007352</td>\n",
       "      <td>0.007352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-03-31</th>\n",
       "      <td>0.997943</td>\n",
       "      <td>-0.002057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04-30</th>\n",
       "      <td>1.015404</td>\n",
       "      <td>0.015404</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-05-31</th>\n",
       "      <td>0.964279</td>\n",
       "      <td>-0.035721</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-30</th>\n",
       "      <td>0.999219</td>\n",
       "      <td>-0.000781</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>330 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Return_plus1    Return\n",
       "Day                               \n",
       "1996-01-31      0.967693 -0.032307\n",
       "1996-02-29      1.029013  0.029013\n",
       "1996-03-31      1.006239  0.006239\n",
       "1996-04-30      1.224249  0.224249\n",
       "1996-05-31      0.944932 -0.055068\n",
       "...                  ...       ...\n",
       "2023-02-28      1.007352  0.007352\n",
       "2023-03-31      0.997943 -0.002057\n",
       "2023-04-30      1.015404  0.015404\n",
       "2023-05-31      0.964279 -0.035721\n",
       "2023-06-30      0.999219 -0.000781\n",
       "\n",
       "[330 rows x 2 columns]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Return_plus1']=data_new['Return']+1\n",
    "#原来的收益率加一，也就是data_new['Close']/data_new['Preclose']\n",
    "Month_data2=data_new.resample('M')['Return_plus1'].prod().to_frame()\n",
    "#按月重新选取样本，将收益率加一后选取样本进行连乘(prod)\n",
    "Month_data2['Return']=Month_data2['Return_plus1']-1\n",
    "#相当于是(这个月的最后一天Close/上个月最后一天Close）-1\n",
    "Month_data2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 年度收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$Y$表示年份\n",
    "$$\n",
    "R_{Y}=\\frac{P_{Close,Y}-P_{Close,Y-1}}{P_{Close,Y-1}}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1996-12-31</th>\n",
       "      <td>917.0200</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-12-31</th>\n",
       "      <td>1194.1000</td>\n",
       "      <td>917.0200</td>\n",
       "      <td>0.302153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-12-31</th>\n",
       "      <td>1146.7000</td>\n",
       "      <td>1194.1000</td>\n",
       "      <td>-0.039695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999-12-31</th>\n",
       "      <td>1366.5800</td>\n",
       "      <td>1146.7000</td>\n",
       "      <td>0.191750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>2073.4800</td>\n",
       "      <td>1366.5800</td>\n",
       "      <td>0.517277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>1645.9700</td>\n",
       "      <td>2073.4800</td>\n",
       "      <td>-0.206180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>1357.6500</td>\n",
       "      <td>1645.9700</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>1497.0400</td>\n",
       "      <td>1357.6500</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>1266.5000</td>\n",
       "      <td>1497.0400</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1266.5000</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>2675.4700</td>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>5261.5600</td>\n",
       "      <td>2675.4700</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>1820.8100</td>\n",
       "      <td>5261.5600</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>3277.1400</td>\n",
       "      <td>1820.8100</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>2808.0800</td>\n",
       "      <td>3277.1400</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>2199.4200</td>\n",
       "      <td>2808.0800</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>2269.1300</td>\n",
       "      <td>2199.4200</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>2115.9800</td>\n",
       "      <td>2269.1300</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>3234.6800</td>\n",
       "      <td>2115.9800</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>3539.1800</td>\n",
       "      <td>3234.6800</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>3103.6400</td>\n",
       "      <td>3539.1800</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>3307.1700</td>\n",
       "      <td>3103.6400</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>2493.9000</td>\n",
       "      <td>3307.1700</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>3050.1200</td>\n",
       "      <td>2493.9000</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>3473.0700</td>\n",
       "      <td>3050.1200</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>3639.7800</td>\n",
       "      <td>3473.0700</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>3089.2579</td>\n",
       "      <td>3639.7800</td>\n",
       "      <td>-0.151251</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>3202.0623</td>\n",
       "      <td>3089.2579</td>\n",
       "      <td>0.036515</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose    Return\n",
       "Day                                       \n",
       "1996-12-31   917.0200        NaN       NaN\n",
       "1997-12-31  1194.1000   917.0200  0.302153\n",
       "1998-12-31  1146.7000  1194.1000 -0.039695\n",
       "1999-12-31  1366.5800  1146.7000  0.191750\n",
       "2000-12-31  2073.4800  1366.5800  0.517277\n",
       "2001-12-31  1645.9700  2073.4800 -0.206180\n",
       "2002-12-31  1357.6500  1645.9700 -0.175167\n",
       "2003-12-31  1497.0400  1357.6500  0.102670\n",
       "2004-12-31  1266.5000  1497.0400 -0.153997\n",
       "2005-12-31  1161.0600  1266.5000 -0.083253\n",
       "2006-12-31  2675.4700  1161.0600  1.304334\n",
       "2007-12-31  5261.5600  2675.4700  0.966593\n",
       "2008-12-31  1820.8100  5261.5600 -0.653941\n",
       "2009-12-31  3277.1400  1820.8100  0.799825\n",
       "2010-12-31  2808.0800  3277.1400 -0.143131\n",
       "2011-12-31  2199.4200  2808.0800 -0.216753\n",
       "2012-12-31  2269.1300  2199.4200  0.031695\n",
       "2013-12-31  2115.9800  2269.1300 -0.067493\n",
       "2014-12-31  3234.6800  2115.9800  0.528691\n",
       "2015-12-31  3539.1800  3234.6800  0.094136\n",
       "2016-12-31  3103.6400  3539.1800 -0.123062\n",
       "2017-12-31  3307.1700  3103.6400  0.065578\n",
       "2018-12-31  2493.9000  3307.1700 -0.245911\n",
       "2019-12-31  3050.1200  2493.9000  0.223032\n",
       "2020-12-31  3473.0700  3050.1200  0.138667\n",
       "2021-12-31  3639.7800  3473.0700  0.048001\n",
       "2022-12-31  3089.2579  3639.7800 -0.151251\n",
       "2023-12-31  3202.0623  3089.2579  0.036515"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Year_data=data_new.resample('Y')['Close'].last().to_frame()\n",
    "Year_data['Preclose']=Year_data['Close'].shift(1)\n",
    "Year_data['Return']=(Year_data['Close']/Year_data['Preclose'])-1\n",
    "Year_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此处类比以上月度收益率算法一"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 中国股票市场年平均回报率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.11448447074562128"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_return = Year_data['Return'].mean()\n",
    "#mean表示求均值\n",
    "mean_return"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
